/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dbEditor.java
 *
 * Created on 26/08/2011, 19:34:20
 */

package fallen.deck.db_editor;

import fallen.deck.entity.*;
import fallen.deck.hibernate.Queries;
import java.awt.Color;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import csm.uttils.generics.GenericTableModel;
import java.util.Iterator;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class dbEditor extends javax.swing.JFrame {

    /** Creates new form dbEditor */
    public dbEditor() {
        initComponents();
        loadCombos();
        loadLists();  
        loadTable();
    }

    private void checkDataBase() {
        if (Queries.getExpansions().size() == 0)
            DB_Init.resetDataBase();
    }
    
    public void loadTable()
    {
        List cards = Queries.getCards();
        String[] cols = {"Expansion","Num", "Carta", "Senda"};
        String[][] rows = new String[cards.size()][4];
        int i = 0;
        Iterator cardIterator = cards.iterator();
        while (cardIterator.hasNext()) {
            Card card = (Card)cardIterator.next();
            rows[i][0] = card.getExpansion().getName();
            rows[i][1] = String.valueOf(card.getNumber());
            rows[i][2] = card.getName();
            rows[i][3] = card.getPathway().getName(); 
            i++;
        }
        GenericTableModel tModel = new GenericTableModel(cols, rows);
        tableCards.setModel(tModel);
        tableCards.repaint();
    }

    private void cleanForm() {
        txtName.setText("Nueva Carta");
        txtNumber.setText("");
        txtaEfect.setText("");
        txtaText.setText("");
        txtCost.setText("");
        txtFUE.setText("");
        txtRES.setText("");
        cboExpansion.setSelectedIndex(0);
        cboArtist1.setSelectedIndex(0);
        cboArtist2.setSelectedIndex(0);
        cboExpansion.setSelectedIndex(0);
        cboFrequency.setSelectedIndex(0);
        cboPathway.setSelectedIndex(0);
        cboType.setSelectedIndex(0);
        lstSubtypes.removeSelectionInterval(0, lstSubtypes.getModel().getSize());
        lstSupertypes.removeSelectionInterval(0, lstSupertypes.getModel().getSize());
        txtName.setSelectionStart(0);
        txtName.setSelectionEnd(txtName.getText().length()); 
        txtName.requestFocus();
    }

    private void disableAllTypeCaracteristics() {
        txtCost.setEnabled(false);
        txtFUE.setEnabled(false);
        txtRES.setEnabled(false);
        lstSubtypes.setEnabled(false);
        lstSupertypes.setEnabled(false);
        txtCost.setText("");
        txtFUE.setText("");
        txtRES.setText("");
        lstSubtypes.removeSelectionInterval(0, lstSubtypes.getModel().getSize());
        lstSupertypes.removeSelectionInterval(0, lstSupertypes.getModel().getSize());
    }

    private void enableAllTypeCaracteristics() {
        txtCost.setEnabled(true);
        txtFUE.setEnabled(true);
        txtRES.setEnabled(true);
        lstSubtypes.setEnabled(true);
        lstSupertypes.setEnabled(true);
    }

    private ArrayList getSelectedCards() throws NumberFormatException {
        ArrayList cards = new ArrayList();
        for (int row : tableCards.getSelectedRows()) {
            Expansion exp = new Expansion(tableCards.getModel().getValueAt(row, 0).toString());
            int num = Integer.parseInt((String)tableCards.getModel().getValueAt(row, 1));
            cards.add(Queries.getCard(exp, num));
        }
        
        return cards;
    }

    private void loadExpansions() {
        List expansions = Queries.getExpansions();
        cboExpansion.removeAllItems();
        for (int i = 0; expansions.size() > i; i++) {
            cboExpansion.addItem(expansions.get(i));
        }
    }

    private void loadLists() {
        loadSupertypes();
        loadSubtypes();
    }

    private void loadCombos() {
        loadExpansions();
        loadTypes();
        loadPathways();
        loadFrequencies();
        loadFirstArtist();
    }

    private void loadFirstArtist() {
        List artists1 = Queries.getArtists();
        cboArtist1.removeAllItems();
        for (int i = 0; artists1.size() > i; i++) {
            cboArtist1.addItem(artists1.get(i));
        }
    }

    private void loadFrequencies() {
        List frequencies = Queries.getFrequencies();
        cboFrequency.removeAllItems();
        for (int i = 0; frequencies.size() > i; i++) {
            cboFrequency.addItem(frequencies.get(i));
        }
    }

    private void loadPathways() {
        List pathways = Queries.getPathways();
        cboPathway.removeAllItems();
        for (int i = 0; pathways.size() > i; i++) {
            cboPathway.addItem(pathways.get(i));
        }
    }

    private void loadSecondArtist() {
        List artists2 = Queries.getArtists();
        cboArtist2.removeAllItems();
        cboArtist2.addItem("[No Seleccionado]");
        for (int i = 0; artists2.size() > i; i++) {
            if (((Artist) artists2.get(i)).getId() == ((Artist) cboArtist1.getSelectedItem()).getId()) {
                continue;
            }
            cboArtist2.addItem(artists2.get(i));
        }
    }

    private void loadTypes() {
        List types = Queries.getTypes();
        cboType.removeAllItems();
        for (int i = 0; types.size() > i; i++) {
            cboType.addItem(types.get(i));
        }
    }

    private void loadSubtypes() {
        List subtypes = Queries.getSubtypes();
        lstSubtypes.removeAll();
        DefaultListModel listModel = new DefaultListModel();
        for (int i = 0; subtypes.size() > i; i++) {
            listModel.addElement(subtypes.get(i));
        }
        lstSubtypes.setModel(listModel);
    }

    private void loadSupertypes() {
        List supertypes = Queries.getSupertypes();
        lstSupertypes.removeAll();
        DefaultListModel listModel = new DefaultListModel();
        for (int i = 0; supertypes.size() > i; i++) {
            listModel.addElement(supertypes.get(i));
        }
        lstSupertypes.setModel(listModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane = new javax.swing.JTabbedPane();
        pnlDatabase = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableCards = new javax.swing.JTable();
        btnRemoveCard = new javax.swing.JButton();
        pnlNewCard = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        lblEfect = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        cboType = new javax.swing.JComboBox();
        lblType = new javax.swing.JLabel();
        lblText = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtaEfect = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtaText = new javax.swing.JTextArea();
        btnSave = new javax.swing.JButton();
        btnClean = new javax.swing.JButton();
        lblPathway = new javax.swing.JLabel();
        cboPathway = new javax.swing.JComboBox();
        lblFrequency = new javax.swing.JLabel();
        cboFrequency = new javax.swing.JComboBox();
        lblArtist1 = new javax.swing.JLabel();
        lblArtist2 = new javax.swing.JLabel();
        cboArtist1 = new javax.swing.JComboBox();
        cboArtist2 = new javax.swing.JComboBox();
        pnlAliado = new javax.swing.JPanel();
        lblSupertypes = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        lstSupertypes = new javax.swing.JList();
        lblSubtypes = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        lstSubtypes = new javax.swing.JList();
        lblCost = new javax.swing.JLabel();
        lblStr = new javax.swing.JLabel();
        lblRes = new javax.swing.JLabel();
        txtCost = new javax.swing.JTextField();
        txtFUE = new javax.swing.JTextField();
        txtRES = new javax.swing.JTextField();
        lblExpnasion = new javax.swing.JLabel();
        cboExpansion = new javax.swing.JComboBox();
        lblNumber = new javax.swing.JLabel();
        txtNumber = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Drecker 2.0 - Editor de Base de Datos");
        setResizable(false);

        tableCards.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableCards.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tableCardsKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(tableCards);

        btnRemoveCard.setText("Borrar Carta");
        btnRemoveCard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveCardActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlDatabaseLayout = new javax.swing.GroupLayout(pnlDatabase);
        pnlDatabase.setLayout(pnlDatabaseLayout);
        pnlDatabaseLayout.setHorizontalGroup(
            pnlDatabaseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatabaseLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatabaseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
                    .addComponent(btnRemoveCard, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        pnlDatabaseLayout.setVerticalGroup(
            pnlDatabaseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDatabaseLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRemoveCard)
                .addContainerGap())
        );

        jTabbedPane.addTab("Base de Datos", pnlDatabase);

        lblName.setText("Nombre:");

        lblEfect.setText("Efecto:");

        txtName.setText("Nueva Carta");
        txtName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNameKeyTyped(evt);
            }
        });

        cboType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboTypeActionPerformed(evt);
            }
        });

        lblType.setText("Tipo:");

        lblText.setText("Texto:");

        txtaEfect.setColumns(20);
        txtaEfect.setLineWrap(true);
        txtaEfect.setRows(5);
        jScrollPane2.setViewportView(txtaEfect);

        txtaText.setColumns(20);
        txtaText.setLineWrap(true);
        txtaText.setRows(5);
        jScrollPane3.setViewportView(txtaText);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/accept.png"))); // NOI18N
        btnSave.setText("Registrar");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnClean.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/textfield.png"))); // NOI18N
        btnClean.setText("Limpiar");
        btnClean.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCleanActionPerformed(evt);
            }
        });

        lblPathway.setText("Senda:");

        cboPathway.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lblFrequency.setText("Frequencia:");

        cboFrequency.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lblArtist1.setText("Artista 1:");

        lblArtist2.setText("Artista 2:");

        cboArtist1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboArtist1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboArtist1ActionPerformed(evt);
            }
        });

        cboArtist2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        pnlAliado.setBorder(javax.swing.BorderFactory.createTitledBorder("Características del Tipo:"));

        lblSupertypes.setText("Super-Tipos:");

        lstSupertypes.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane4.setViewportView(lstSupertypes);

        lblSubtypes.setText("Sub-Tipos:");

        lstSubtypes.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane5.setViewportView(lstSubtypes);

        lblCost.setText("Coste:");

        lblStr.setText("FUE:");

        lblRes.setText("RES:");

        javax.swing.GroupLayout pnlAliadoLayout = new javax.swing.GroupLayout(pnlAliado);
        pnlAliado.setLayout(pnlAliadoLayout);
        pnlAliadoLayout.setHorizontalGroup(
            pnlAliadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAliadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlAliadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblSupertypes)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlAliadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnlAliadoLayout.createSequentialGroup()
                        .addComponent(lblSubtypes)
                        .addGap(78, 78, 78)
                        .addComponent(lblCost))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlAliadoLayout.createSequentialGroup()
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(pnlAliadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblStr)
                            .addComponent(lblRes))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlAliadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtRES, 0, 0, Short.MAX_VALUE)
                    .addComponent(txtFUE, 0, 0, Short.MAX_VALUE)
                    .addComponent(txtCost, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE))
                .addGap(314, 314, 314))
        );
        pnlAliadoLayout.setVerticalGroup(
            pnlAliadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAliadoLayout.createSequentialGroup()
                .addGroup(pnlAliadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSupertypes)
                    .addComponent(lblSubtypes)
                    .addComponent(lblCost)
                    .addComponent(txtCost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlAliadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlAliadoLayout.createSequentialGroup()
                        .addGroup(pnlAliadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblStr)
                            .addComponent(txtFUE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlAliadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRes)
                            .addComponent(txtRES, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                .addContainerGap())
        );

        lblExpnasion.setText("Expansión:");

        cboExpansion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lblNumber.setText("Número:");

        javax.swing.GroupLayout pnlNewCardLayout = new javax.swing.GroupLayout(pnlNewCard);
        pnlNewCard.setLayout(pnlNewCardLayout);
        pnlNewCardLayout.setHorizontalGroup(
            pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlNewCardLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlNewCardLayout.createSequentialGroup()
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnClean))
                    .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlNewCardLayout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(pnlAliado, 0, 469, Short.MAX_VALUE))
                        .addGroup(pnlNewCardLayout.createSequentialGroup()
                            .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(pnlNewCardLayout.createSequentialGroup()
                                    .addGap(22, 22, 22)
                                    .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(lblEfect)
                                        .addComponent(lblText))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)))
                                .addGroup(pnlNewCardLayout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(lblName)
                                        .addComponent(lblNumber))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                                        .addComponent(txtNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlNewCardLayout.createSequentialGroup()
                                    .addGap(29, 29, 29)
                                    .addComponent(lblPathway))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlNewCardLayout.createSequentialGroup()
                                    .addGap(5, 5, 5)
                                    .addComponent(lblExpnasion))
                                .addGroup(pnlNewCardLayout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblType))
                                .addComponent(lblFrequency)
                                .addGroup(pnlNewCardLayout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblArtist1))
                                .addGroup(pnlNewCardLayout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblArtist2)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(cboType, 0, 140, Short.MAX_VALUE)
                                .addComponent(cboPathway, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cboExpansion, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cboFrequency, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cboArtist1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cboArtist2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap(55, Short.MAX_VALUE))
        );
        pnlNewCardLayout.setVerticalGroup(
            pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlNewCardLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlNewCardLayout.createSequentialGroup()
                        .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblName, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNumber))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblEfect)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                            .addComponent(lblText)))
                    .addGroup(pnlNewCardLayout.createSequentialGroup()
                        .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblExpnasion)
                            .addComponent(cboExpansion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPathway)
                            .addComponent(cboPathway, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblType)
                            .addComponent(cboType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblFrequency)
                            .addComponent(cboFrequency, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblArtist1)
                            .addComponent(cboArtist1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblArtist2)
                            .addComponent(cboArtist2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlAliado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlNewCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnClean))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane.addTab("Carta", pnlNewCard);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void txtNameKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNameKeyTyped
        if (txtName.getText().length() == 0) txtName.setBackground(Color.red);
        else txtName.setBackground(Color.white);
    }//GEN-LAST:event_txtNameKeyTyped

    private void btnCleanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCleanActionPerformed
        cleanForm();
        loadCombos();
    }//GEN-LAST:event_btnCleanActionPerformed

    private void cboArtist1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboArtist1ActionPerformed
        if (cboArtist1.getSelectedIndex() >= 0) loadSecondArtist();
    }//GEN-LAST:event_cboArtist1ActionPerformed

private void cboTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboTypeActionPerformed
    if (cboType.getSelectedIndex() < 0) return;
    
    if(((Type)cboType.getSelectedItem()).getName().compareToIgnoreCase("Aliado") == 0)
        {
            enableAllTypeCaracteristics();
        }
    else if (((Type)cboType.getSelectedItem()).getName().compareToIgnoreCase("Arma") == 0)
    {
        enableAllTypeCaracteristics();
        lstSubtypes.setEnabled(false);
    }
        else if (((Type)cboType.getSelectedItem()).getName().compareToIgnoreCase("Ritual") == 0||
                ((Type)cboType.getSelectedItem()).getName().compareToIgnoreCase("Objeto") == 0)
        {
            disableAllTypeCaracteristics();
            txtCost.setEnabled(true);
            lstSupertypes.setEnabled(true);
        }
        else
        {
            disableAllTypeCaracteristics();            
        }
}//GEN-LAST:event_cboTypeActionPerformed

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (!validateFields()) return;
        
        Card newCard = new Card(txtName.getText());
        newCard.setNumber(Integer.parseInt(txtNumber.getText()));
        newCard.setEffect(txtaEfect.getText());
        newCard.setText(txtaText.getText());
        newCard.setCost(txtCost.getText());
        newCard.setStr(txtFUE.getText());
        newCard.setRes(txtRES.getText());
        newCard.setExpansion((Expansion)cboExpansion.getSelectedItem());
        newCard.setFrequency((Frequency)cboFrequency.getSelectedItem());
        newCard.setPathway((Pathway)cboPathway.getSelectedItem());
        List artists = new ArrayList();
        artists.add((Artist)cboArtist1.getSelectedItem());
        if (cboArtist2.getSelectedIndex() > 0)
            artists.add((Artist)cboArtist2.getSelectedItem());
        newCard.setArtist(artists);
        List type = new ArrayList();
        type.add((Type)cboType.getSelectedItem());
        List superTypes = new ArrayList();
        for ( int i = 0; lstSupertypes.getSelectedValues().length > i; i++)
        {
            superTypes.add((Supertype)lstSupertypes.getModel().
                    getElementAt(lstSupertypes.getSelectedIndices()[i]));
        }
        newCard.setSupertype(superTypes);
        List subTypes = new ArrayList();
        for ( int i = 0; lstSubtypes.getSelectedValues().length > i; i++)
        {
            subTypes.add((Subtype)lstSubtypes.getModel().
                    getElementAt(lstSubtypes.getSelectedIndices()[i]));
        }
        newCard.setSubtype(subTypes);
        newCard.setType(type);
        
        if (Queries.save(newCard))
        {
            JOptionPane.showMessageDialog(this, "La carta "+ newCard.getName() 
                    + " se guardo correctamente.", "Resultado de la Operacion",
                    JOptionPane.INFORMATION_MESSAGE);
            cleanForm();
        }
        else
        {
            JOptionPane.showMessageDialog(this, "La carta "+ newCard.getName() 
                    + " no se pudo guardar.", "Resultado de la Operacion",
                    JOptionPane.WARNING_MESSAGE);
        }
        
        loadTable();
}//GEN-LAST:event_btnSaveActionPerformed

private void btnRemoveCardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveCardActionPerformed
        if(tableCards.getSelectedRow() < 0) return;
        ArrayList selectedCard = getSelectedCards();
        for (Object card : selectedCard.toArray())
        {
            Queries.remove((Card)card);
        }        
        loadTable();
}//GEN-LAST:event_btnRemoveCardActionPerformed

private void tableCardsKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tableCardsKeyTyped
        //if (evt.getKeyCode() == Character.)
}//GEN-LAST:event_tableCardsKeyTyped

    private boolean validateFields() throws HeadlessException {
        boolean result = true;
        if (txtName.getText().compareToIgnoreCase("") == 0)
            {
                JOptionPane.showMessageDialog(this, "Debes ingresar un nombre a la carta!",
                        "Formulario Incompleto", JOptionPane.OK_OPTION);
                txtName.requestFocus();
                result = false;
            }
            else if (txtNumber.getText().compareToIgnoreCase("") == 0)
            {
                JOptionPane.showMessageDialog(this, "Debes ingresar un numero a la carta!",
                        "Formulario Incompleto", JOptionPane.OK_OPTION);
                txtNumber.requestFocus();
                result = false;
            }
            else if (txtCost.isEnabled() &&
                    txtCost.getText().compareToIgnoreCase("") == 0)
            {
                JOptionPane.showMessageDialog(this, "Debes ingresar un costo a la carta!",
                        "Formulario Incompleto", JOptionPane.OK_OPTION);
                txtCost.requestFocus();
                result = false;
            }
            else if (txtFUE.isEnabled() &&
                    txtFUE.getText().compareToIgnoreCase("") == 0)
            {
                JOptionPane.showMessageDialog(this, "Debes ingresar un costo a la carta!",
                        "Formulario Incompleto", JOptionPane.OK_OPTION);
                txtFUE.requestFocus();
                result = false;
            }
            else if (txtRES.isEnabled() &&
                    txtRES.getText().compareToIgnoreCase("") == 0)
            {
                JOptionPane.showMessageDialog(this, "Debes ingresar un costo a la carta!",
                        "Formulario Incompleto", JOptionPane.OK_OPTION);
                txtRES.requestFocus();
                result = false;
            }
        
        try
        {
            Integer.getInteger(txtNumber.getText());
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(this, "Debes ingresar un numero valido a la carta!",
                        "Formulario Incompleto", JOptionPane.OK_OPTION);
                txtNumber.requestFocus();
            result = false;
        }
        return result;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dbEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dbEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dbEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dbEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new dbEditor().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClean;
    private javax.swing.JButton btnRemoveCard;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cboArtist1;
    private javax.swing.JComboBox cboArtist2;
    private javax.swing.JComboBox cboExpansion;
    private javax.swing.JComboBox cboFrequency;
    private javax.swing.JComboBox cboPathway;
    private javax.swing.JComboBox cboType;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JLabel lblArtist1;
    private javax.swing.JLabel lblArtist2;
    private javax.swing.JLabel lblCost;
    private javax.swing.JLabel lblEfect;
    private javax.swing.JLabel lblExpnasion;
    private javax.swing.JLabel lblFrequency;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblNumber;
    private javax.swing.JLabel lblPathway;
    private javax.swing.JLabel lblRes;
    private javax.swing.JLabel lblStr;
    private javax.swing.JLabel lblSubtypes;
    private javax.swing.JLabel lblSupertypes;
    private javax.swing.JLabel lblText;
    private javax.swing.JLabel lblType;
    private javax.swing.JList lstSubtypes;
    private javax.swing.JList lstSupertypes;
    private javax.swing.JPanel pnlAliado;
    private javax.swing.JPanel pnlDatabase;
    private javax.swing.JPanel pnlNewCard;
    private javax.swing.JTable tableCards;
    private javax.swing.JTextField txtCost;
    private javax.swing.JTextField txtFUE;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtNumber;
    private javax.swing.JTextField txtRES;
    private javax.swing.JTextArea txtaEfect;
    private javax.swing.JTextArea txtaText;
    // End of variables declaration//GEN-END:variables

}
